/**
 * @description Json数据操作功能
 * @namespace bs.json对转功能包
 * @type {{}|*}
 */
;bs.json = bs.json || {};
/**
 * Json对象转为字符串
 * @param object
 * @returns {*}
 */
bs.json.toString = function(object){
     if(bs.util.isKeyExists('bs.jsonEncode')){
         return __toString(object);
     }else{
         var _p = bs.using.base + 'data';
         var _m = {
             js: _p + 'bs.json.js'
         }
         bs.using.modules.BsJson = _m;
         bs.using.load('BsJson', function () {
             return __toString(object);
         });
     }
     function __toString(object){
         return bs.jsonEncode(object);
     }
};
/**
 * Json格式字符串转为Json对象
 * @param str
 * @returns {*}
 */
bs.json.toObject = function (str) {
    if (bs.util.isKeyExists('bs.jsonDecode')) {
        return __toObject(str);
    } else {
        var _p = bs.using.base + 'data';
        var _m = {
            js: _p + 'bs.json.js'
        }
        bs.using.modules.BsJson = _m;
        bs.using.load('BsJson', function () {
            return __toObject(str);
        });
    }
    function __toObject(str) {
        return bs.jsonDecode(str);
    }
};
jQuery.extend({
    /**
     * @see  将javascript数据类型转换为json字符串
     * @param 待转换对象,支持object,array,string,function,number,boolean,regexp
     * @return 返回json字符串
     */
    toJSON: function (object) {
        var type = typeof object;
        if ('object' == type) {
            if (Array == object.constructor)
                type = 'array';
            else if (RegExp == object.constructor)
                type = 'regexp';
            else
                type = 'object';
        }
        switch (type) {
            case 'undefined':
            case 'unknown':
                return;
                break;
            case 'function':
            case 'boolean':
            case 'regexp':
                return object.toString();
                break;
            case 'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case 'string':
                return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g,
                    function () {
                        var a = arguments[0];
                        return  (a == '\n') ? '\\n' :
                            (a == '\r') ? '\\r' :
                                (a == '\t') ? '\\t' : ""
                    }) + '"';
                break;
            case 'object':
                if (object === null) return 'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSON(object[property]);
                    if (value !== undefined)
                        results.push(jQuery.toJSON(property) + ':' + value);
                }
                return '{' + results.join(',') + '}';
                break;
            case 'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSON(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return '[' + results.join(',') + ']';
                break;
        }
    }
});